package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;//菜品

    @Autowired
    private SetmealService setmealService;//套餐
    @Override
    public void deleteById(Long id) {
        //1.根据分类id去查询菜品
        //select count（*）from dish where cotegory_id=?
        Integer count1 = dishService.lambdaQuery().eq(Dish::getCategoryId, id).count();
        if(count1>0){
            throw new CustomerException("该分类不能删除,有【菜品】关联数据");

        }
        //2.根据分类id去查询套餐
        //select * from setmeal where cotegory_id=?
        Integer count2 = setmealService.lambdaQuery().eq(Setmeal::getCategoryId, id).count();
        if(count2>0){
            throw new CustomerException("该分类不能删除,有【套餐】关联数据");

        }
        //3.根据分类id删除数据，this表示CategoryServiceImpl这个对象，super表示父类
        //delete from cotegory where id=?
        this.removeById(id);
    }
}
